//
//  NSDictionary+NN.m
//  TempletProject
//
//  Created by lina on 15/11/4.
//  Copyright © 2015年 张 坤. All rights reserved.
//

#import "NSDictionary+NN.h"
#import "NSObject+NN.h"

@implementation NSDictionary (NN)

- (NSString *)JSONString
{
    NSError * error;
    NSData *data = [NSJSONSerialization dataWithJSONObject:self options:0 error:&error];
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

+ (instancetype)dictionaryWithJSONString:(NSString *)string
{
    NSData *resultData = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    return [NSJSONSerialization JSONObjectWithData:resultData
                                           options:NSJSONReadingAllowFragments
                                             error:&error];
}
+ (NSString *)logDic:(NSDictionary *)dic {
    if (![dic count]) {
        return nil;
    }
    NSString *tempStr1 =
    [[dic description] stringByReplacingOccurrencesOfString:@"\\u"
                                                 withString:@"\\U"];
    NSString *tempStr2 =
    [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *tempStr3 =
    [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString *str =
    [NSPropertyListSerialization propertyListFromData:tempData
                                     mutabilityOption:NSPropertyListImmutable
                                               format:NULL
                                     errorDescription:NULL];
    return str;
}
-(id)safeObjectForKey:(id)aKey {
    id value = [self objectForKey:aKey];
    if ([NSObject isNilOrNull:value]) {
        return nil;
    }
    return value;
}

//+(void)maskNSNull {
//    [RSSwizzle swizzleInstanceMethod:@selector(objectForKey:) inClass:NSClassFromString(@"__NSCFDictionary") newImpFactory:^id(RSSwizzleInfo *swizzleInfo) {
//        return ^id(__unsafe_unretained id self, id key){
//            
//            id (*originalIMP)(__unsafe_unretained id, SEL, id);
//            originalIMP = (__typeof(originalIMP))[swizzleInfo getOriginalImplementation];
//            id res = originalIMP(self,@selector(objectForKey:),key);
//            if ([res isKindOfClass:[NSNull class]]) {
//                return nil;
//            }
//            return res;
//        };
//    } mode:RSSwizzleModeAlways key:nil];
//    
//    [RSSwizzle swizzleInstanceMethod:@selector(objectForKey:) inClass:NSClassFromString(@"__NSDictionaryM") newImpFactory:^id(RSSwizzleInfo *swizzleInfo) {
//        return ^id(__unsafe_unretained id self, id key){
//            
//            id (*originalIMP)(__unsafe_unretained id, SEL, id);
//            originalIMP = (__typeof(originalIMP))[swizzleInfo getOriginalImplementation];
//            id res = originalIMP(self,@selector(objectForKey:),key);
//            if ([res isKindOfClass:[NSNull class]]) {
//                return nil;
//            }
//            return res;
//        };
//    } mode:RSSwizzleModeAlways key:nil];
//}

@end
